﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.BLL;
using System.Text;
using KeRui.Common;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using KeRui.TemplateMaker;

public partial class Manage_Cms_ContentList : System.Web.UI.Page
{
    protected string navName;
    protected int nodeID;
    public int PageSize = 15;                               //每页数量
    public int PageIndex = PageRequest.GetInt("page", 1);   //请求的页码
    public int PageCS = PageRequest.GetInt("cs", -1);       //记录总数
    public int PageCount;                                   //总页数
    public string PageShow = "";                            //分页显示
    public string PageUrl = "";                             //分页参数连接

    protected string deleteShow;
    protected string restoreShow = "display:none;";
    protected List<KeRui.Model.CmsContent> listCmsContent;

    public string act = PageRequest.GetString("act").ToLower().Trim();              //动作，删除delete 还原restore 修复？check
    public string DataType = PageRequest.GetString("datatype").ToLower().Trim();    //类型，审核verify 回收站recylebin
    public int SiteID = PageRequest.GetInt("siteid", -1);
    public int NodeID = PageRequest.GetInt("nodeid", -1);
    public int ContentID = PageRequest.GetInt("contentid", -1);
    public string ContentIDs = PageRequest.GetString("ContentIDs");
    public int sContentID = PageRequest.GetInt("scontentid", -1);

    public int IsRecommend = PageRequest.GetInt("isrecommend", 0);
    public int IsHot = PageRequest.GetInt("ishot", 0);
    public int IsMarked = PageRequest.GetInt("ismarked", 0);
    public int IsTop = PageRequest.GetInt("istop", 0);
    public int IsAll = PageRequest.GetInt("isall", 0);

    public string Title = PageRequest.GetString("title").Trim();
    public string StartTime = PageRequest.GetString("starttime");
    public string EndTime = PageRequest.GetString("endtime");
    ManageErrMsg nsb = new ManageErrMsg();
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (ContentID > 1)
        //    Response.Redirect("ContentItem.aspx?contentid=" + ContentID + "&nodeid="+NodeID+"&siteid="+SiteID);
        //检查登录HYC
        ManageHelper.CheckAdminLogin();
        //检查是否有管理权限HYC
        ManageHelper.CheckAdminPower("site_contentlist_" + SiteID);
        //检查是否有对栏目的权限
        if (NodeID != -1)
        {
            //ManageHelper.CheckAdminPower(SiteID + "_" + NodeID + "node");
        }
        KeRui.BLL.CmsContent bllCmsContent = new KeRui.BLL.CmsContent();
        KeRui.BLL.CmsNode bllCmsNode = new CmsNode();
        KeRui.BLL.CmsSite bllSite = new CmsSite();

        PageUrl += "&DataType=" + DataType.ToString();
        PageUrl += "&SiteID=" + SiteID.ToString();
        PageUrl += "&NodeID=" + NodeID.ToString();

        if (sContentID > 0)
            PageUrl += "&sContentID=" + sContentID.ToString();

        PageUrl += "&IsRecommend=" + IsRecommend.ToString();
        PageUrl += "&IsHot=" + IsHot.ToString();
        PageUrl += "&IsMarked=" + IsMarked.ToString();
        PageUrl += "&IsTop=" + IsTop.ToString();
        PageUrl += "&IsAll=" + IsAll.ToString();

        PageUrl += "&Title=" + Title.ToString();
        PageUrl += "&StartTime=" + StartTime.ToString();
        PageUrl += "&EndTime=" + EndTime.ToString();

        #region 处理 act
        switch (act)
        {
            case "delete":
                //删除文件
                //检查是否有删除权限HYC
                ManageHelper.CheckAdminPower("site_contentdelete_" + SiteID);

                string dir = bllCmsNode.GetModel(bllCmsContent.GetModel(ContentID).NodeID).NodeDir;
                string file = dir + "\\" + PageRequest.GetInt("contentid", -1) + ".shtml";
                if (File.Exists(file))
                {
                    File.Delete(file);

                    int i = 2;
                    string PageFile = dir + "\\" + PageRequest.GetInt("contentid", -1) + "_" + i + ".shtml";
                    while (File.Exists(PageFile))
                    {
                        File.Delete(PageFile);
                        i++;
                        PageFile = dir + "\\" + PageRequest.GetInt("contentid", -1) + "_" + i + ".shtml";
                    }
                }
                bllCmsContent.FixPreNextID(ContentID, "delete");
                bllCmsContent.Delete(ContentID);

                ManageHelper.AddLog("site_contentdelete", "在站点" + SiteID + "下删除了ID为" + ContentID + "的新闻");
                Response.Redirect("ContentList.aspx?cs=" + PageCS + "&page=" + PageIndex + PageUrl);
                Response.End();
                break;
            case "restore":
                bllCmsContent.Delete(PageRequest.GetInt("contentid", -1));
                Response.Redirect("ContentList.aspx?cs=" + PageCS + "&page=" + PageIndex + PageUrl);
                Response.End();
                break;
            case "check":
                bllCmsContent.Check(PageRequest.GetInt("contentid", -1));
                Publish publish = new Publish();
                publish.PublishContent(PageRequest.GetInt("contentid", -1));
                Response.Redirect("ContentList.aspx?cs=" + PageCS + "&page=" + PageIndex + PageUrl);
                Response.End();
                break;
            case "deletemore":
                //HYC编写
                //检查是否有删除权限HYC
                ManageHelper.CheckAdminPower("site_contentdelete_" + SiteID);
                string[] IDs = Utils.SplitString(ContentIDs, ",");
                for (int i = 0; i < IDs.Length-1; i++)
                {
                    string dirIDs = bllCmsNode.GetModel(bllCmsContent.GetModel(Convert.ToInt32(IDs[i])).NodeID).NodeDir;
                    string fileIDs = dirIDs + "\\" + Convert.ToInt32(IDs[i]) + ".shtml";
                    if (File.Exists(fileIDs))
                    {
                        File.Delete(fileIDs);

                        int deli = 2;
                        string PageFile = dirIDs + "\\" + Convert.ToInt32(IDs[i]) + "_" + deli + ".shtml";
                        while (File.Exists(PageFile))
                        {
                            File.Delete(PageFile);
                            deli++;
                            PageFile = dirIDs + "\\" + Convert.ToInt32(IDs[i]) + "_" + deli + ".shtml";
                        }
                    }
                    bllCmsContent.FixPreNextID(Convert.ToInt32(IDs[i]), "delete");
                    bllCmsContent.Delete(Convert.ToInt32(IDs[i]));
                }
                ManageHelper.AddLog("site_contentdelete", "在站点" + SiteID + "下批量删除了ID为" + ContentIDs + "的信息");
                Response.Redirect("ContentList.aspx?cs=" + PageCS + "&page=" + PageIndex + PageUrl);
                Response.End();
                break;
            case "deletenodeall":
                //删除本节点下的全部.shtml
                ManageHelper.CheckAdminPower("site_contentdelete_" + SiteID);
                dir = bllCmsNode.GetModel(NodeID).NodeDir;
                string[] foldernames= Directory.GetFiles(dir,"*.shtml");//获得所有.shtml文件
                foreach (string foldername in foldernames)//删除开始
                {
                    if (File.Exists(foldername))
                    {
                        File.Delete(foldername);
                    }
                 }
                //前往数据库，删除本节点下信息
                bllCmsContent.DeleteByNodeID(NodeID);
                ManageHelper.AddLog("site_contentdelete", "在站点" + SiteID + "下删除了节点为：" + NodeID + "下的所有.shtml文件");
                Response.Redirect("ContentList.aspx?cs=" + PageCS + "&page=" + PageIndex + PageUrl);
                Response.End();
                break;
        }
        #endregion


        KeRui.Model.CmsSite modelSite = bllSite.GetModelByCache(SiteID);
        if (modelSite == null)
        {
            Response.Write("SiteID站点编号丢失。");
            Response.End();
        }


        //首次则获取根节点
        #region navName
        //KeRui.Model.CmsNode modelCmsNode = bllCmsNode.GetList("SiteID=" + SiteID.ToString() + " AND ParentID=0")[0];
        //if (NodeID == -1)
        //    NodeID = modelCmsNode.NodeID;

        KeRui.Model.CmsNode modelCmsNode = bllCmsNode.GetModelByCache(NodeID);
        if (modelCmsNode == null)
            modelCmsNode = bllCmsNode.GetModelByCache(modelSite.SiteID, "/");
        NodeID = modelCmsNode.NodeID;
        if (NodeID <= 0)
        {
            Response.Write("NodeID节点编号丢失。");
            Response.End();
        }

        nodeID = NodeID;//兼容旧页面的nodeID

        if (NodeID == -1)
        {
            navName = "首 页";
        }
        else if (modelCmsNode.Depth == 0)
        {
            navName = "首 页";
        }
        else
        {
            StringBuilder stringBuilder = new StringBuilder();
            string[] arrChilds = modelCmsNode.ParentPath.Split(',');
            foreach (string parentnode in arrChilds)
            {
                if (parentnode != "0")
                {
                    stringBuilder.AppendFormat("{0} > ", bllCmsNode.GetModelByCache(Convert.ToInt32(parentnode)).NodeName);
                }
            }
            navName = stringBuilder.Append(modelCmsNode.NodeName).ToString();
        }
        #endregion


        #region strWhere
        string strWhere = "";
        if (NodeID > 0 && IsAll == 0)
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "NodeID =" + NodeID + "";
        }
        if (sContentID > 0)
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "ContentID =" + sContentID + "";
        }

        #region Is
        if (IsRecommend > 0)
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "IsRecommend =" + IsRecommend + "";
        }
        if (IsHot > 0)
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "IsHot =" + IsHot + "";
        }
        if (IsMarked > 0)
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "IsMarked =" + IsMarked + "";
        }
        if (IsTop > 0)
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "IsTop =" + IsTop + "";
        }
        #endregion

        if (Title != "")
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "Title like '%" + Utils.SqlStringFormat(Title, 2) + "%'";
        }

        if (StartTime != "" & Utils.IsDateTime(StartTime))
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "PublishTime >='" + Utils.SqlStringFormat(StartTime) + "'";
        }

        if (EndTime != "" & Utils.IsDateTime(EndTime))
        {
            if (strWhere != "")
                strWhere += " and ";
            strWhere += "PublishTime <='" + Utils.SqlStringFormat(EndTime) + "'";
        }

        #endregion

        switch (DataType)
        {
            case "verify":
                {
                    if (strWhere != "")
                        strWhere += " and ";
                    strWhere += "ContentState<>2  AND IsDelete=0";
                }
                //listCmsContent = bllCmsContent.GetList(" NodeID=" + KeRui.Common.PageRequest.GetInt("nodeid", -1) + " AND ContentState<>2  AND IsDelete='False' ", PageSize, PageIndex);
                break;
            case "recylebin":
                {
                    if (strWhere != "")
                        strWhere += " and ";
                    strWhere += "IsDelete=1";
                }
                //listCmsContent = bllCmsContent.GetList(" NodeID=" + KeRui.Common.PageRequest.GetInt("nodeid", -1) + " AND IsDelete='True' ", PageSize, PageIndex);
                deleteShow = "display:none;";
                restoreShow = string.Empty;
                break;
            default:
                {
                    if (strWhere != "")
                        strWhere += " and ";
                    strWhere += "IsDelete=0";
                }
                //listCmsContent = bllCmsContent.GetList(" NodeID=" + KeRui.Common.PageRequest.GetInt("nodeid", -1) + " AND IsDelete='False' ", PageSize, PageIndex);
                break;
        }

        #region 旧代码
        //case "search":
        //    int contentID = PageRequest.GetInt("contentid", -1);
        //    string title = PageRequest.GetString("title");
        //    string istop = PageRequest.GetString("istop");
        //    string isrecommend = PageRequest.GetString("isrecommend");
        //    string ishot = PageRequest.GetString("ishot");
        //    string ismarked = PageRequest.GetString("ismarked");
        //    int cs = PageRequest.GetInt("cs", -1);
        //    int page = PageRequest.GetInt("page", -1);
        //    int siteID = KeRui.Common.PageRequest.GetInt("siteid", -1);
        //    int nodeid = KeRui.Common.PageRequest.GetInt("nodeid", -1);
        //    string dataType = PageRequest.GetString("datatype");
        //    string strWhere;
        //    switch (PageRequest.GetString("datatype"))
        //    {
        //        case "verify":
        //            strWhere = "AND ContentState<>2  AND IsDelete='False' ";
        //            break;
        //        case "recylebin":
        //            strWhere = " AND IsDelete='True' ";
        //            deleteShow = "display:none;";
        //            restoreShow = string.Empty;
        //            break;
        //        default:
        //            strWhere = " AND IsDelete='False'";
        //            break;
        //    }
        //    string strWhere2 = string.Empty;
        //    if (contentID != -1)
        //        strWhere2 = "ContentID=" + contentID;
        //    if (!string.IsNullOrEmpty(title))
        //        strWhere2 += " AND Title LIKE '%" + title + "%'";
        //    if (istop == "1")
        //        strWhere2 += " AND IsTop='True'";
        //    if (isrecommend == "1")
        //        strWhere2 += " AND isrecommend='True'";
        //    if (ishot == "1")
        //        strWhere2 += " AND ishot='True'";
        //    if (ismarked == "1")
        //        strWhere2 += " AND ismarked='True'";
        //    if (!string.IsNullOrEmpty(strWhere2) && contentID != -1)
        //        strWhere2 = " AND " + strWhere2;
        //    listCmsContent = bllCmsContent.GetList(string.Format("NodeID=" + KeRui.Common.PageRequest.GetInt("nodeid", -1) + "{0} {1} ", strWhere2, strWhere), PageSize, page);
        //    //Response.Redirect(string.Format("ContentList.aspx?siteid={0}&nodeid={1}&datatype={2}&cs={3}&page={4}&contentid={5}&title={6}&istop={7}&isrecommend={8}&ishot={9}&ismarked={10}", siteID, nodeid, dataType, cs, page, contentID, title, istop, isrecommend, ishot, ismarked));
        //    Response.End();
        //    break;
        #endregion

        //Response.Write("strWhere:" + strWhere + "<br>PageSize:" + PageSize + "<br>PageIndex:" + PageIndex + "<br>");
        //Response.End();

        listCmsContent = bllCmsContent.GetList(strWhere, PageSize, PageIndex);

        //总数
        if (PageCS == -1)
            PageCS = bllCmsContent.GetCount(strWhere); //bllCmsContent.GetList(" NodeID=" + KeRui.Common.PageRequest.GetInt("nodeid", -1) + " AND IsDelete='False' ", -1, -1).Count;
        //总页数
        if (PageCS % PageSize == 0)
            PageCount = PageCS / PageSize;
        else
            PageCount = PageCS / PageSize + 1;
        //分页显示
        PageShow = ManageHelper.showSplitPage("ContentList.aspx?cs=" + PageCS + "&page={0}" + PageUrl, PageIndex, PageSize, PageCS);
    }

    public static string GetUserName()
    {
        KeRui.BLL.Admin bllAdmin = new Admin();
        return bllAdmin.GetModel(KeRui.BLL.Account.GetLoginUserID()).RealName;
    }


    public static string hText(string str, string txt)
    {
        if (str.Length == 0)
            return "";
        if (txt.Length == 0)
            return str;
        //【妆点屋衣橱】米娜推荐09韩版秋热门新品<span class=H>新款</span>抽绳短裤热裤
        //return str.Replace(txt, "<span class=H>" + txt + "</span>");

        //return Regex.Replace(str, txt, "<span class=H>" + txt + "</span>", options);

        string ret = str.Trim();
        string[] keywords = Utils.SplitString(txt, " ");
        for (int keyi = 0; keyi < keywords.Length; keyi++)
        {
            ret = Regex.Replace(ret, keywords[keyi], "<span style='color:#F50!important;'>" + keywords[keyi] + "</span>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        }

        return ret;

    }
}